<?php
class sprintsActions extends sfGraduaActions
{

    public function executeIndex($request)
    {
        $this->sprints = SprintsService::getAll();
        sfLoader::loadHelpers(array('Partial','Php2Js'));
        $this->sprintViewTpl  = partial2Js('sprints/sprintView.jstpl',array(),true);
        $this->sprintFormTpl  = partial2Js('sprints/sprintForm.jstpl',array(),true);
        $this->editDecorator   = partial2Js('globalPartials/editDecorator.jstpl',array(),true);
        $this->insertDecorator = partial2Js('globalPartials/insertDecorator.jstpl',array(),true);
        $this->viewDecorator   = partial2Js('globalPartials/viewDecoratorOver.jstpl',array(),true);
        $this->listTpl         = partial2Js('globalPartials/listTpl.jstpl',array(),true);
        $this->divListItemDecorator   = partial2Js('globalPartials/divListItemDecorator.jstpl',array(),true);
        $this->setLayout('layoutTasks');
    }

    function executeInsert($request) {
        $data = $this->getObjRequest('sprint');
        $jsObjName = $this->getRequestParameter('jsObjName');
        $this->getResponse()->setTransferErrorFieldNames(array(
          'name','description','start_date','end_date'
        ));
        $this->getResponse()->setFieldIdPrefix('sprint');
        $id = SprintsService::insert($data);
        $this->getResponseJsCall()
            ->addCommand('sprints.list', 'refresh')
            ->addPHPParam('itemsDs', SprintsService::getAll() )
            ->closeDialog()
            ->showFeedback('Inserido com sucesso!');
        return sfView::NONE;
    }

    function executeEdit($request) {
        $data = $this->getObjRequest('sprint',array('use_tests'));
        $jsObjName = $this->getRequestParameter('jsObjName');
        $this->getResponse()->setTransferErrorFieldNames(array(
          'name','description','start_date','end_date'
        ));
        $this->getResponse()->setFieldIdPrefix('sprint');
        //$('#projectForm-1 [name="project[name]"]').val('*ooo*')
        SprintsService::edit($data);
        $this->getResponseJsCall()
            ->addCommand($jsObjName, 'update')
            ->addPHPParam('ds', SprintsService::get($data->id))
            ->showFeedback('Sucesso na edição.');
        return sfView::NONE;
    }

    function executeRemove($request) {
        //Dbg::x($this->getRequest()->getParameterHolder());
        $id = $this->getRequestParameter('id');
        $jsObjName = $this->getRequestParameter('jsObjName');
        SprintsService::delete($id);
        $this->getResponseJsCall()
            ->addCommand('sprints.list', 'refresh')
            ->addPHPParam('itemsDs', SprintsService::getAll() )
            ->closeDialog()
            ->showFeedback('Removido com sucesso!');
        return sfView::NONE;
    }


    function executeShow($request) {
        sfLoader::loadHelpers(array('Partial','Php2Js'));
        $id = $this->getRequestParameter('id');
        $sprintTasks = SprintsService::getSprintTasks($id);
        $this->getResponseJsCall()
            ->addCommand('sprints', 'displaySprint')
            ->addPhpParam(
                'data',
                partial2Js(
                    'sprints/showSprint',
                    array(
                      'sprintTasks'=>$sprintTasks, 'sprint'=>$sprintTasks['sprint']
                    ),
                    false
                )
            );
        
        return sfView::NONE;
    }

}
